"""UI相关的衍生配置处理"""

from typing import Dict, Any
from ...type.types import MySQLTypeInfo
from ...type.converters import MySQLToMiaomaTypeConverter
from ...type.type_info import MIAOMA_TYPE_INFO_MAP

def derive_ui_config(meta_info: Dict[str, Any]) -> Dict[str, Any]:
    """处理meta.json的UI相关衍生配置

    处理字段级衍生配置:
    - columns.*.derived.ui.vue_antd_component: Ant Design Vue组件名称

    :param meta_info: meta.json格式的结构体
    :return: 更新后的结构体,可以继续传递给其他derived函数处理
    """
    # 遍历处理每个column
    for column_name, column_info in meta_info["columns"].items():
        try:
            # 从column_info中提取类型信息
            sql_type = column_info["sql"]["type"]
            type_info = MySQLTypeInfo(
                name=sql_type["name"].upper(),  # 确保类型名称大写
                length=sql_type.get("length"),
                precision=sql_type.get("precision"),
                scale=sql_type.get("scale")
            )

            # 转换为MiaomaType
            miaoma_type = MySQLToMiaomaTypeConverter.convert(
                mysql_type=type_info.name,  # 已经转换为大写
                length=type_info.length,
                precision=type_info.precision,
                scale=type_info.scale
            )

            # 获取对应的vue_antd_component
            miaoma_type_info = MIAOMA_TYPE_INFO_MAP[miaoma_type]
            vue_antd_component = miaoma_type_info.vue_antd_component

            # 添加derived配置
            if "derived" not in column_info:
                column_info["derived"] = {}
            if "ui" not in column_info["derived"]:
                column_info["derived"]["ui"] = {}

            # 设置antd_component
            column_info["derived"]["ui"]["vue_antd_component"] = vue_antd_component

        except Exception as e:
            # 如果转换失败,使用默认组件a-input
            if "derived" not in column_info:
                column_info["derived"] = {}
            if "ui" not in column_info["derived"]:
                column_info["derived"]["ui"] = {}
            column_info["derived"]["ui"]["vue_antd_component"] = "a-input"

    return meta_info